package models

import "time"

type CommentFeedback struct {
	ID          int
	UserId      int
	MediaUserId int
	CId         int
	Type        int
	Content     string
	Status      int
	CreatedAt   time.Time
	UpdatedAt   time.Time
	Operation string `json:"operation"`
	Replay string `json:"replay"`
}

func (c *CommentFeedback) TableName() string {
	return "comment_feedback"
}

type CommentFeedbackResult struct {
	Id        int       `json:"id"`
	Phone     string    `json:"phone"`
	Informant string    `json:"informant"`
	Criminal  string    `json:"criminal"`
	CreatedAt time.Time `json:"created_at"`
	Status    int       `json:"status"`
	Type      string       `json:"item"`
}

func GetCommentFeedbackList(item string, status string, criminal string, beginTime string, endTime string ,page int, pageSize int) ([]*CommentFeedbackResult, *Paginator) {
	model := DB.Table("comment_feedback as c").Select("c.id,u.phone,ud.wechat_name as informant,ui.operators_name as criminal,c.type,c.status,c.created_at").Joins("left join user_detail ud on ud.user_id=c.user_id").Joins("left join media_user_info as ui on ui.media_user_id=c.media_user_id").Joins("left join users as u on u.id=c.user_id")
	if item != ""{
		model = model.Where("c.type like ?", "%"+item+"%")
	}
	if status != "" {
		model = model.Where("c.status=?", status)
	}
	if criminal != "" {
		model = model.Where("ui.operators_name like ?", "%"+criminal+"%")
	}
	if beginTime != "" {
		model = model.Where("c.created_at > ?", beginTime)
	}
	if endTime != "" {
		model = model.Where("c.created_at < ?", endTime)
	}
	var count int
	var result []*CommentFeedbackResult
	model.Count(&count)
	paginate := NewPage(page, pageSize, count)
	err := model.Offset(paginate.Offset).Limit(paginate.Limit).Order("c.created_at desc").Find(&result).Error
	if err != nil {
		panic(err)
	}
	return result, &paginate
}

type CommentFeedbackOne struct {
	ID int `json:"id"`
	Phone string `json:"phone"`
	Name string `json:"name"`
	Target string `json:"target"`
	CreatedAt time.Time `json:"created_at"`
	Quote string `json:"quote"`
	Type int `json:"type"`
	Content string `json:"content"`
	Status int `json:"status"`
	Operation string `json:"operation"`
	Replay string `json:"replay"`
}

func CommentFeedbackGet(id int) *CommentFeedbackOne{
	var res CommentFeedback
	DB.Model(&CommentFeedback{}).Where("id=?", id).First(&res)
	var phone string
	DB.Table("users").Select("phone").Where("id=?", res.UserId).Row().Scan(&phone)
	var name string
	DB.Table("user_detail").Select("wechat_name").Where("user_id=?",res.UserId).Row().Scan(&name)
	var target string
	DB.Table("media_user_info").Select("operators_name").Where("media_user_id=?",res.MediaUserId).Row().Scan(&target)
	var quote string
	DB.Table("articles").Select("title").Where("id=?",res.CId).Row().Scan(&quote)
	return &CommentFeedbackOne{res.ID,phone,name,target,res.CreatedAt,quote,res.Type,res.Content,res.Status,res.Operation,res.Replay}
}